package com.dragon.www.haier.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.dragon.www.haier.R;
import com.dragon.www.haier.util.StringUtil;

/**
 * Created by 51268 on 2016/3/11.
 */
public class BaseItemView extends RelativeLayout {
    private static String NAMESPACE = "http://schemas.android.com/apk/res-auto";
    private ImageView imageViewOn;
    private ImageView imageViewOff;
    private ImageView imageViewLightOn;
    private ImageView imageViewLightOff;
    private boolean listenerflag = true;
    private String stringOnID;
    private String stringOffID;
    private String stringLightOnID;
    private String stringLightBackground;
    private ImageView imageViewBackground;

    public BaseItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public BaseItemView(Context context, AttributeSet attrs) {
        super(context, attrs);

//        for (int i = 0; i < attrs.getAttributeCount(); i++) {
//            System.out.println(attrs.getAttributeName(i) + ":  " + attrs.getAttributeValue(i));
//        }

        stringOnID=attrs.getAttributeValue(NAMESPACE,"on");
        stringOffID=attrs.getAttributeValue(NAMESPACE,"off");
        stringLightOnID=attrs.getAttributeValue(NAMESPACE,"light_on");
        stringLightBackground=attrs.getAttributeValue(NAMESPACE,"light_background");

        initView();
    }

    public BaseItemView(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        View view = View.inflate(getContext(), R.layout.base_view_item, this);
        imageViewOn = (ImageView) view.findViewById(R.id.image_on);
        imageViewOff = (ImageView) view.findViewById(R.id.image_off);
        imageViewBackground = (ImageView) view.findViewById(R.id.on_off_background);
        imageViewLightOn = (ImageView) view.findViewById(R.id.image_light_on);
        imageViewLightOff = (ImageView) view.findViewById(R.id.image_light_off);
        RelativeLayout relativeBaseView = (RelativeLayout) view.findViewById(R.id.relative_baseView);
        if(stringOnID!=null){
            int onID=Integer.parseInt(StringUtil.splitResouceID(stringOnID));
            System.out.println("offID" + onID);
            imageViewOn.setImageResource(onID);
        }
        if(stringOffID!=null){
            int offID=Integer.parseInt(StringUtil.splitResouceID(stringOffID));
            System.out.println("onID"+offID);
            imageViewOff.setImageResource(offID);
        }
        if(stringLightOnID!=null){
            int lightOnID=Integer.parseInt(StringUtil.splitResouceID(stringLightOnID));
            imageViewLightOn.setImageResource(lightOnID);
        }
        if(stringLightBackground!=null){
            int lightBackground=Integer.parseInt(StringUtil.splitResouceID(stringLightBackground));
            imageViewBackground.setImageResource(lightBackground);
        }
    }

    public boolean controlOnOff() {
        if (this.imageViewLightOn.getVisibility() == VISIBLE) {
            //本身是开，再点击，就调用关闭的方法
            turnOff();
            return false;
        } else {
            turnOn();
            return true;
        }
    }

    public void turnOn() {
        //打开
        imageViewOff.setVisibility(INVISIBLE);
        imageViewLightOff.setVisibility(INVISIBLE);
        imageViewOn.setVisibility(VISIBLE);
        imageViewLightOn.setVisibility(VISIBLE);
    }

    public void turnOff() {
        //关闭
        imageViewOn.setVisibility(INVISIBLE);
        imageViewLightOn.setVisibility(INVISIBLE);
        imageViewOff.setVisibility(VISIBLE);
        imageViewLightOff.setVisibility(VISIBLE);
    }

}


